package com.android.readcard.read;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.android.readcard.bean.CardData;
import com.android.readcard.bean.ReadCardInfo;
import com.android.readcard.inter.UpLoadListener;
import com.android.readcard.utils.BleGlobUtils;
import com.android.readcard.utils.UploadCardUtils;
import com.identity.Shell;
import com.identity.globalEnum;

import java.io.IOException;


public class ShenSiConnectUtils {
    private BluetoothDevice remoteDevice;
    private String bleAddress;
    private String bleName;
    private Context context;
    private Activity activity;

    private String name, sex, nation, id_num, address, sign_office, to, from, born;
    private String birth_year, birth_month, birth_day, sign_data, photoHeard;
    private Bitmap photo;
    private String bluetoothName = "";
    // 注册成功失败监听
    private UpLoadListener mOnUpLoadListener;

    private Shell shell = null;
    private String bluetoothType;

    public ShenSiConnectUtils(Context context, String bluetoothType, BluetoothDevice remoteDevice,
                              String bleName, String bleAddress, final String bluetoothName,
                              final UpLoadListener mOnUpLoadListener) {
        this.context = context;
        this.activity = (Activity) context;
        this.bluetoothType = bluetoothType;
        this.remoteDevice = remoteDevice;
        this.bleName = bleName;
        this.bleAddress = bleAddress;
        this.bluetoothName = bluetoothName;
        this.mOnUpLoadListener = mOnUpLoadListener;
    }
    public void connect() {
        try {
            shenSiRead();
            if (shell == null) {
                shell = new Shell(context, remoteDevice);
            }
        } catch (IOException e) {
            setErr("与机具建立连接失败");
            e.printStackTrace();
        }
    }
    public void shenSiRead() {
        try {
            if (shell == null) {
                shell = new Shell(context, remoteDevice);
            }
            boolean bRet = false;
            byte[] cardInfo = new byte[256];
            globalEnum ge = globalEnum.NONE;
            ge = shell.Init();
            if (ge == globalEnum.INITIAL_SUCCESS) {
                bRet = shell.id2ShakeHand();
                if (bRet) {
                    int nRet = shell.id2ReadCard();
                    if (nRet == 0) {
                        cardInfo = shell.GetCardInfoBytes();
                        String wltPath = "/data/data/com.android.readcard/files/";
                        String termBPath = "/mnt/sdcard/";
                        int nret = shell.GetPic(wltPath, termBPath);
                        Log.e("获取读卡参数", "------printStackTrace==::::" + nret);
                        if (nret > 0) {
                            // 身份证头像
                            photo = BitmapFactory.
                                    decodeFile("/data/data/com.android.readcard/files/zp.bmp");
                            photoHeard = BleGlobUtils.replaceBlank(BleGlobUtils.bitmapToBase64(photo));
                            name = shell.GetName(cardInfo).toString().trim();
                            sex = shell.GetGender(cardInfo).toString().trim();
                            nation = shell.GetNational(cardInfo).toString().trim();
                            born = shell.GetBirthday(cardInfo).toString().trim();
                            birth_year = born.substring(0, 4);
                            birth_month = born.substring(5, 7);
                            birth_day = born.substring(8, 10);
                            address = shell.GetAddress(cardInfo).toString().trim();
                            id_num = shell.GetIndentityCard(cardInfo).toString().trim();
                            sign_office = shell.GetIssued(cardInfo).toString().trim();
                            from = shell.GetStartDate(cardInfo).toString().trim();
                            to = shell.GetEndDate(cardInfo).toString().trim();
                            String fromYear = from.substring(0, 4);
                            String formMonth = from.substring(5, 7);
                            String fromDay = from.substring(8, 10);
                            if (to.contains("长期")) {
                                sign_data = fromYear + "." + formMonth + "." + fromDay
                                        + "--" + to.trim();
                            } else {
                                String toYear = to.substring(0, 4);
                                String toMonth = to.substring(5, 7);
                                String toDay = to.substring(8, 10);
                                sign_data = fromYear + "." + formMonth + "." + fromDay
                                        + "--" + toYear + "." + toMonth + "." + toDay;
                            }

                            String chuanMa = UploadCardUtils.getChuanMa(bleName, bleAddress);// 获取串码
// 获取身份证数据
                            ReadCardInfo mReadCardInfo = UploadCardUtils.getCardInfo(bluetoothName, address, photoHeard, sex, id_num, nation,
                                    sign_office, to, name, from, born, sign_data, birth_year,
                                    birth_month, birth_day, photo);

                            // 获取读卡上传数据,使用时需要把data转换成json和串码一起上传
                            CardData mCardData = UploadCardUtils.getData(bluetoothType, address, photoHeard, sex, id_num, nation,
                                    sign_office, to, name, from, born, sign_data, birth_year,
                                    birth_month, birth_day, photo);

                            mOnUpLoadListener.onSucUpLoad(mReadCardInfo, mCardData, chuanMa);

                            activity.deleteFile("zp.bmp");
                        } else if (nret == -5) {
                            setErr("照片解码授权文件不正确");
                        } else if (nret == -1) {
                            setErr("照片原始数据不正确");
                        }
                    } else {
                        shell.Destroy();
                        setErr("与设备握手失败,nRet=" + nRet);
                    }
                } else {
                    setErr("与设备握手失败,bRet=" + bRet);
                }
            } else {
                shell.Destroy();
                setErr("与机具建立连接失败a");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("获取读卡参数", "------printStackTrace-----" + e.toString());
            setErr("与机具建立连接失败");
        }
    }

    private void setErr(String err) {
        mOnUpLoadListener.onErrUpLoad(3, err);
    }

    /**
     * 断开连接
     */
    public void stopConnect() {
        try {
            shell.Destroy();
            Log.e("项目异常神思", "------神思断开连接------" + "");
        } catch (Exception e) {
            e.printStackTrace();
            setErr("断开连接异常");
        }

    }
}
